package com.cooba.content_frament;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.activitys.login.LoginActivity;
import com.cooba.activitys.setting.SettingActivity;
import com.cooba.utils.MySqlOpenHelper;

/**
 * Created by Administrator on 2015/12/7.
 * 我
 */
public class MeFrament extends Fragment implements View.OnClickListener {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    private View contentview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (contentview == null) {
            contentview = initview(inflater);
        }
        return contentview;
    }

    //头像
    private FrameLayout photo;
    //设置
    private ImageView mIv_setting;
    //昵称
    private TextView tvname;
    //初始化控件
    private View initview(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.me_frament, null, false);

        photo = (FrameLayout) view.findViewById(R.id.photo);
        tvname= (TextView) view.findViewById(R.id.id_name);

        mIv_setting = (ImageView) view.findViewById(R.id.iv_setting);
//读取数据
        getCount();
        
        
        photo.setOnClickListener(this);
        mIv_setting.setOnClickListener(this);
        return view;
    }

    private void getCount() {
        MySqlOpenHelper helper = new MySqlOpenHelper(getActivity());
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor cursor = db.query("login_date", null, null, null, null, null, null, null);
        if (cursor.moveToFirst()){
            String name=cursor.getString(cursor.getColumnIndex("name"));
            String phone=cursor.getString(cursor.getColumnIndex("phone"));
            if (phone.equals("")) {
                tvname.setText("游客登录");
            } else {
                tvname.setText(name);
            }
        }
        cursor.close();
        db.close();
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.photo:
                intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_setting:
                intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
        }
    }
}
